﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.JointVariableLimitsEditor.CUiJVLEditorColorBucketControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;


namespace DnProcessSimulateCommands.JointVariableLimitsEditor;

internal class CUiJVLEditorColorBucketControl : Button
{
  private Color _selectedColor = Color.Green;
  private Color _originalBucketColor = Color.Empty;
  private IContainer components;
  private ImageList _bucketImageList;

  public event EventHandler SelectedColorChanged;

  public CUiJVLEditorColorBucketControl()
  {
    this.InitializeComponent();
    this.updateBucketColor();
  }

  internal Color SelectedColor
  {
    get => this._selectedColor;
    set
    {
      if (!(this._selectedColor != value))
        return;
      this._selectedColor = value;
      this.updateBucketColor();
      this.fireSelectedColorChanged();
    }
  }

  private void updateBucketColor()
  {
    if (this._originalBucketColor.IsEmpty)
      this.initOriginalBucketColor();
    Image image = this._bucketImageList.Images[0];
    ColorMap[] map = new ColorMap[1];
    ColorMap colorMap = new ColorMap();
    colorMap.OldColor = this._originalBucketColor;
    Color color = this._selectedColor;
    if (color == this._bucketImageList.TransparentColor)
      color = color.B <= (byte) 0 ? Color.FromArgb((int) color.A, (int) color.R, (int) color.G, (int) color.B + 1) : Color.FromArgb((int) color.A, (int) color.R, (int) color.G, (int) color.B - 1);
    colorMap.NewColor = color;
    map[0] = colorMap;
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetRemapTable(map);
    Bitmap bitmap = new Bitmap(image);
    Rectangle destRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
    Graphics graphics = Graphics.FromImage((Image) bitmap);
    graphics.PageUnit = GraphicsUnit.Pixel;
    graphics.DrawImage(image, destRect, 0, 0, destRect.Width, destRect.Height, GraphicsUnit.Pixel, imageAttr);
    this.Image = (Image) bitmap;
  }

  private void initOriginalBucketColor()
  {
    using (Bitmap bitmap = new Bitmap(this._bucketImageList.Images[0]))
      this._originalBucketColor = bitmap.GetPixel(1, bitmap.Height - 1);
  }

  private void fireSelectedColorChanged()
  {
    if (this.SelectedColorChanged == null)
      return;
    this.SelectedColorChanged((object) this, new EventArgs());
  }

  protected override void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    this.components = (IContainer) new System.ComponentModel.Container();
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiJVLEditorColorBucketControl));
    this._bucketImageList = new ImageList(this.components);
    this.SuspendLayout();
    this._bucketImageList.ImageStream = (ImageListStreamer) componentResourceManager.GetObject("_bucketImageList.ImageStream");
    this._bucketImageList.TransparentColor = Color.Transparent;
    this._bucketImageList.Images.SetKeyName(0, "modifyColor.ico");
    this.ResumeLayout(false);
  }
}
